package cn.edu.scau.cmi.wangmaoren.client.classicsDesignPattern;

import af.javafx.layout.AfLinearLayout;
import cn.edu.scau.cmi.wangmaoren.simpleFactoryMethod.MeatFactory;
import cn.edu.scau.cmi.wangmaoren.simpleFactoryMethod.domain.Meat;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SimpleFactoryMethodClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();
	private Button producePorkBotton = new Button("生产猪肉");
	private Button produceChickenBotton = new Button("生产鸡肉");
	private Button produceBeefBotton = new Button("生产牛肉");
	private Label leftBlank = new Label();
	private Label rightBlank = new Label();
	private TextArea inforText = new TextArea();

	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();// 初始化界面

			Scene scene = new Scene(root, 750, 463);
			primaryStage.setTitle("简单工厂模式");
			primaryStage.setScene(scene);
			primaryStage.show();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}
	
	public void openWindow()
	{
		initLayout();//初始化界面
		
		Scene scene = new Scene(root,750,463);
		Stage stage = new Stage();
		stage.setTitle("简单工厂模式");
		stage.setScene(scene);
		stage.show();
	}

	/*
	 * 生产猪肉
	 */
	private void producePork()
	{
		Meat pork = MeatFactory.getMeat("pork");
		inforText.appendText("\n" + pork.cook() + "\n");
	}

	/*
	 * 生产牛肉
	 */
	private void produceBeef()
	{
		Meat beef = MeatFactory.getMeat("beef");
		inforText.appendText("\n" + beef.cook() + "\n");
	}

	/*
	 * 生产鸡肉
	 */
	private void produceChicken()
	{
		Meat chicken = MeatFactory.getMeat("chicken");
		inforText.appendText("\n" + chicken.cook() + "\n");
	}

	/*
	 * 初始化界面
	 */
	private void initLayout()
	{
		root.orientation(Orientation.VERTICAL).spacing(20).padding(20);
		line.spacing(38);

		line.getChildren().addAll(leftBlank, producePorkBotton, produceBeefBotton, produceChickenBotton, rightBlank);
		line.layout("1 190px 190px 190px 1");
		root.add(line, "50px");
		root.add(inforText, "1");

		producePorkBotton.setFont(Font.font("华文中宋", 20));
		produceBeefBotton.setFont(Font.font("华文中宋", 20));
		produceChickenBotton.setFont(Font.font("华文中宋", 20));
		inforText.setFont(Font.font("华文中宋", 18));

		Image porkImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/simpleFactoryMethod/pork.png"));
		producePorkBotton.setGraphic(new ImageView(porkImage));
		Image beefImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/simpleFactoryMethod/beef.png"));
		produceBeefBotton.setGraphic(new ImageView(beefImage));
		Image chickenImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/simpleFactoryMethod/chicken.png"));
		produceChickenBotton.setGraphic(new ImageView(chickenImage));

		producePorkBotton.setOnAction((e) -> {
			producePork();
		});
		produceBeefBotton.setOnAction((e) -> {
			produceBeef();
		});
		produceChickenBotton.setOnAction((e) -> {
			produceChicken();
		});

	}
}

/*
 * public class SimpleFactoryMethodClient { public static void main(String[]
 * args) { Meat beaf = MeatFactory.getMeat("beef"); beaf.cook();
 * 
 * Meat chicken = MeatFactory.getMeat("chicken"); chicken.cook();
 * 
 * Meat pork = MeatFactory.getMeat("pork"); pork.cook(); } }
 */
